#include <func.h>

int main(int argc, char* argv[])
{
    struct addrinfo hints;
    bzero(&hints, sizeof(hints));   // 将其它成员置为0
    hints.ai_family = AF_INET;
    hints.ai_socktype = 0;          // UDP or TCP
    
    struct addrinfo* head;

    int err = getaddrinfo("www.jd.com", "http", &hints, &head);
    if (err) {
        fprintf(stderr, "getaddrinfo(): %s\n", gai_strerror(err));
        exit(1);
    }

    // 打印地址信息
    char ipstr[40];
    struct sockaddr_in6* addr = (struct sockaddr_in6*) head;
    inet_ntop(AF_INET6, &addr->sin6_addr, ipstr, 40);
    int port = ntohs(addr->sin6_port);
    printf("%s: %d\n", ipstr, port);

    printf("---------------------------\n");

    // 指针之间的转换
    struct sockaddr* p1 = (struct sockaddr*)addr;
    struct sockaddr_in6* p2 = (struct sockaddr_in6*) p1;

    inet_ntop(AF_INET6, &p2->sin6_addr, ipstr, 40);
    port = ntohs(p2->sin6_port);
    printf("%s: %d\n", ipstr, port);
    
    // 结构体的转换
    // struct sockaddr s1 = (struct sockaddr) (*addr);
    // struct sockaddr_in6 s2 = (struct sockaddr_in6) s1;

    // inet_ntop(AF_INET6, &s2.sin6_addr, ipstr, 40);
    // port = ntohs(s2.sin6_port);
    // printf("%s: %d\n", ipstr, port);
    return 0;
}

